From: Keir Fraser Date: Wed, 3 Jun 2009 10:11:50 +0000 (+0100) Subject: blktap: fix empty QCOW images (bug 1430 part 2) X-Git-Tag: archive/raspbian/4.8.0-1+rpi1~1^2~13824 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/success/%22http:/www.example.com/cgi/success?a=commitdiff_plain;h=4b48ffd60a4b9177e90b069024a26ec31f5e9e6b;p=xen.git blktap: fix empty QCOW images (bug 1430 part 2) Empty QCOW images consist of only the L1 table, this results in a file size which is not sector-aligned. Since blktap uses O_DIRECT, the block aligned read of the L1 table will go beyond the end of file and thus returns the actual file size and not the expected length. This patch checks whether at least the L1 table has been read. This should fix bug 1430. Signed-off-by: Andre Przywara --- diff --git a/tools/blktap/drivers/block-qcow.c b/tools/blktap/drivers/block-qcow.c index dd65cd01bb..8027fcaca2 100644 --- a/tools/blktap/drivers/block-qcow.c +++ b/tools/blktap/drivers/block-qcow.c @@ -824,7 +824,7 @@ static int tdqcow_open (struct disk_driver *dd, const char *name, td_flag_t flag l1_table_block = ROUNDUP(l1_table_block, 512); ret = posix_memalign((void **)&buf2, 4096, l1_table_block); if (ret != 0) goto fail; - if (read(fd, buf2, l1_table_block) != l1_table_block) + if (read(fd, buf2, l1_table_block) < l1_table_size + s->l1_table_offset) goto fail; memcpy(s->l1_table, buf2 + s->l1_table_offset, l1_table_size); @@ -878,7 +878,8 @@ static int tdqcow_open (struct disk_driver *dd, const char *name, td_flag_t flag memcpy(buf2 + s->l1_table_offset, s->l1_table, l1_table_size); lseek(fd, 0, SEEK_SET); - if (write(fd, buf2, l1_table_block) != l1_table_block) { + if (write(fd, buf2, l1_table_block) < + l1_table_size + s->l1_table_offset) { DPRINTF("qcow: Failed to write new L1 table\n"); goto fail; }